//
//  HMHomeRetweetView.swift
//  WeiBo
//
//  Created by Apple on 2017/5/31.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit

//  转发微博视图
class HMHomeRetweetView: UIView {
    
    //  记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    //  引用ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            contentLabel.text = statusViewModel?.retweetContent
            
            //  卸载之前的底部约束
            retweetViewBottomConstraint?.deactivate()
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //  显示配图
                pictureView.isHidden = false
                //  有转发微博配图
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
                
                //  重新创建约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            } else {
                //  没有转发微博配图
                //  隐藏配图
                pictureView.isHidden = true
                //  重新创建约束
                self.snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                
            }
           
            
        }
    }

    //  MARK:   --懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.lightGray, fontSize: 13)
        label.numberOfLines = 0
        return label
    }()
    
    //  配图
    private lazy var pictureView: HMHomePictureView = {
        let collectionView = HMHomePictureView()
        //  设置背景色
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK:   --添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //  添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp.bottom).offset(HomeCellMargin)
            make.leading.equalTo(contentLabel)
            //  因为配图的顶部和转发微博的底部等于匹配的底部已经确定一个高度了，在设置高度会有约束冲突，这里设置约束优先级，先设置配图高度
            make.size.equalTo(CGSize(width: 100, height: 100)).priority(.high)
            
        }
        
        //  关键约束
        self.snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
        
    }
   

}
